package org.jeecg.modules.basic.demo.apartment.utils;


import org.jeecg.modules.basic.demo.apartment.entity.ApartmentIdModel;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartmentTreeList;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 对应部门的表,处理并查找树级数据
 * <p>
 *
 * @Author: Steve
 * @Date: 2019-01-22
 */
public class FindsApartmentChildrenUtil {




    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将LwgyBaseApartment类型的list集合转换成LwgyBaseApartmentTreeList类型的集合
     */
    public static List<LwgyBaseApartmentTreeList> wrapTreeDataToTreeList(List<LwgyBaseApartment> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<ApartmentIdModel> idList = new ArrayList<>();
        List<LwgyBaseApartmentTreeList> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            LwgyBaseApartment lwgyBaseApartment = recordList.get(i);
            records.add(new LwgyBaseApartmentTreeList(lwgyBaseApartment));
        }
        List<LwgyBaseApartmentTreeList> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * 获取 ApartmentIdModel
     *
     * @param recordList
     * @return
     */
    public static List<ApartmentIdModel> wrapTreeDataToDepartIdTreeList(List<LwgyBaseApartment> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<ApartmentIdModel> idList = new ArrayList<ApartmentIdModel>();
        List<LwgyBaseApartmentTreeList> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            LwgyBaseApartment depart = recordList.get(i);
            records.add(new LwgyBaseApartmentTreeList(depart));
        }
        findChildren(records, idList);
        return idList;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<LwgyBaseApartmentTreeList> findChildren(List<LwgyBaseApartmentTreeList> recordList,
                                                                List<ApartmentIdModel> departIdList) {
        List<LwgyBaseApartmentTreeList> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            LwgyBaseApartmentTreeList branch = recordList.get(i);
            if (branch.getPid().equals("0")) {
                treeList.add(branch);
                ApartmentIdModel departIdModel = new ApartmentIdModel().convert(branch);
                departIdList.add(departIdModel);
            }
        }
        getGrandChildren(treeList, recordList, departIdList);

        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     * 该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<LwgyBaseApartmentTreeList> treeList, List<LwgyBaseApartmentTreeList> recordList, List<ApartmentIdModel> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            LwgyBaseApartmentTreeList model = treeList.get(i);
            ApartmentIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                LwgyBaseApartmentTreeList m = recordList.get(i1);
                if (m.getPid() != null && m.getPid().equals(model.getId())) {
                    model.getChildren().add(m);
                    ApartmentIdModel dim = new ApartmentIdModel().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<LwgyBaseApartmentTreeList> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            LwgyBaseApartmentTreeList model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("getUUID() = " + getUUID());

    }
    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }
}
